package com.zlf.controller;

import com.zlf.feign.PointFeignClient;
import com.zlf.po.CommonResult;
import com.zlf.po.Wallet;
import com.zlf.po.WalletTransaction;
import com.zlf.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@RefreshScope  //开启动态刷新
@RestController
@RequestMapping("/WalletController")
public class WalletController {
    @Autowired
    private WalletService walletService;

    @Qualifier("com.zlf.feign.PointFeignClient")
    @Autowired
    private PointFeignClient pointFeignClient;

    @PostMapping("/createWallet/{id}/{pass}")         //根据id和pass创建钱包（id用userId）
    public CommonResult createWallet(
            @PathVariable("id") Long id,
            @PathVariable("pass") String pass) {
        int res;
        try{
            walletService.createWallet(id,pass);
            res=pointFeignClient.initPoint(String.valueOf(id)).getCode();
        }catch (Exception e){
            return new CommonResult(500,e.getMessage(),null);
        }
        return new CommonResult(200,"create wallet success",res);
    }
    @GetMapping("/getWallet/{walletId}")        //测试用
    public CommonResult<Wallet> getWallet(@PathVariable("walletId") Long walletId) {
        Wallet wallet=walletService.getWallet(walletId);
        return new CommonResult<>(200,"success",wallet);
    }
    @GetMapping("/getBalance/{walletId}")    //查询余额
    public CommonResult<BigDecimal> getBalance(@PathVariable("walletId") Long walletId){
        BigDecimal balance=walletService.getBalance(walletId);
        return new CommonResult<>(200,"sucess",balance);
    }
    @GetMapping("/listTransactionByWalletId/{WalletId}")    //查看所有账单
    public CommonResult<List> ListTransactionByWalletId(@PathVariable("WalletId") Long WalletId){
        List<WalletTransaction> list=walletService.listTransactionByWalletId(WalletId);
        return new CommonResult<>(200,"success",list);
    }
    @GetMapping("/listTransactionByFromWalletId/{WalletId}")    //仅查看支出
    public CommonResult<List> ListTransactionByFromWalletId(@PathVariable("WalletId") Long WalletId){
        List<WalletTransaction> list=walletService.listTransactionByFromWalletId(WalletId);
        return new CommonResult<>(200,"success",list);
    }
    @GetMapping("/listTransactionByToWalletId/{WalletId}")    //仅查看收入
    public CommonResult<List> ListTransactionByToWalletId(@PathVariable("WalletId") Long WalletId){
        List<WalletTransaction> list=walletService.listTransactionByToWalletId(WalletId);
        return new CommonResult<>(200,"success",list);
    }
    @PostMapping("/transfer/{fromWalletId}/{toWalletId}/{amount}/{pass}")    //交易（支付按钮）（这里需要获取用户id还有商家id，且需要商家id注册一个钱包）
    public CommonResult transfer (@PathVariable("fromWalletId") Long fromWalletId,
                          @PathVariable("toWalletId") Long toWalletId,
                          @PathVariable("amount") BigDecimal amount,
                          @PathVariable("pass") String pass) {
        try {
            walletService.transfer (fromWalletId, toWalletId, amount,pass);
            //下面会自动增加积分
            pointFeignClient.addPoint(String.valueOf(fromWalletId), (int) amount.multiply(BigDecimal.valueOf(10)).setScale( 0, RoundingMode.DOWN).longValue());
        }catch (Exception e){
            return new CommonResult(500, e.getMessage(), null);
        }
       return new CommonResult(200,"transfer success",null);
    }
    @PostMapping("/confirmTransaction/{transferId}")      //确认收货，确认之后收款方支付额才会解冻
    public CommonResult ConfirmTransaction(@PathVariable("transferId") Long transferId){
        try {
            walletService.confirmTransaction(transferId);
        }catch (Exception e){
            return new CommonResult(500, e.getMessage(), null);
        }
        return new CommonResult(200,"success",null);
    }
    @PostMapping("/recharge/{toWalletId}/{amount}/{pass}")    //给钱包充值
    public CommonResult recharge(@PathVariable("toWalletId") Long toWalletId,
                         @PathVariable("amount") BigDecimal amount,
                         @PathVariable("pass") String pass){
        try {
            walletService.recharge(toWalletId,amount,pass);
        }catch (Exception e){
            return new CommonResult(500, e.getMessage(), null);
        }
        return new CommonResult(200,"recharge success",null);
    }
    @PostMapping("/withdraw/{fromWalletId}/{amount}/{pass}")     //取款
    public CommonResult withdraw(@PathVariable("fromWalletId") Long fromWalletId,
                                 @PathVariable("amount") BigDecimal amount,
                                 @PathVariable("pass") String pass){
        BigDecimal res;
        try {
            res= walletService.withdraw(fromWalletId,amount,pass);
        }catch (Exception e){
            return new CommonResult(500, e.getMessage(), null);
        }
        return new CommonResult(200,"withdraw success",res);
    }
    @PostMapping("/getVip/{walletId}/{pass}")     //更新最大欠款额度（并自动开启欠款功能）
    public CommonResult getVip(@PathVariable("walletId") Long walletId,@PathVariable("pass") String pass) {
        try {
            walletService.getVip(walletId,pass);
        }catch (Exception e){
            return new CommonResult(500, e.getMessage(), null);
        }
        return new CommonResult(200,"getVip success",null);
    }
}
